package com.cebbank.yyi.score;

/**
 * @description:
 * @author: yyi
 * @version: 1.0
 **/
import java.time.LocalDate;
import java.time.DayOfWeek;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class ScoreProgram {

    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        boolean validInput = false;
        LocalDate startDate = null;
        LocalDate endDate = null;
        //循环获取控制台的数据 直至输入数据合法
        while (!validInput) {
            System.out.println("请输入开始时间(格式:yyyy-MM-dd):");
            String startTimeString = scanner.nextLine();
            System.out.println("请输入结束时间(格式:yyyy-MM-dd):");
            String endTimeString = scanner.nextLine();
            try {
                startDate = LocalDate.parse(startTimeString, formatter);
                endDate = LocalDate.parse(endTimeString, formatter);
            } catch (DateTimeParseException e) {
                System.out.println("输入的时间格式不正确,请使用yyyy-MM-dd格式");
                continue;
            }
            if (endDate.isAfter(startDate)) {
                //校验通过置为true
                validInput = true;
            } else {
                System.out.println("结束时间必须晚于开始时间，请重新输入。");
            }
        }
        //统计每个得分的天数 key为分值 value为该分值对应的天数
        Map<Integer, Integer> scoreDaysCount = new HashMap<>();
        for (int i = 1; i <= 5; i++) {
            scoreDaysCount.put(i, 0);
        }

        long totalDays = ChronoUnit.DAYS.between(startDate, endDate) + 1;
        //计算总得分
        int totalScore = 0;

        for (LocalDate date = startDate; !date.isAfter(endDate); date = date.plusDays(1)) {
            int score = calculateScore(date);
            scoreDaysCount.put(score, scoreDaysCount.get(score) + 1);
            totalScore += score;
            System.out.println("时间: " + date + ", 得分: " + score);
        }

        System.out.println("总天数:" + totalDays);
        System.out.println("总分数:" + totalScore);

        for (int i = 1; i <= 5; i++) {
            System.out.println("得分为" + i + "分的天数: " + scoreDaysCount.get(i));
        }
    }

    /**
     * 根据规则获取当前的时间的得分
     * param date, 当前的时间
     * **/
    private static int calculateScore(LocalDate date) {
        boolean isMonday = date.getDayOfWeek() == DayOfWeek.MONDAY;
        boolean isFirstDayOfMonth = date.getDayOfMonth() == 1;
        boolean isSunday = date.getDayOfWeek() == DayOfWeek.SUNDAY;
        boolean isLastDayOfMonth = date.equals(date.withDayOfMonth(date.lengthOfMonth()));

        if (isMonday && isFirstDayOfMonth) {
            return 4;
        } else if (isSunday && isFirstDayOfMonth) {
            return 4;
        } else if (isMonday && isLastDayOfMonth) {
            return 5;
        } else if (isSunday && isLastDayOfMonth) {
            return 5;
        } else if (isMonday || isFirstDayOfMonth) {
            return 2;
        } else if (isSunday || isLastDayOfMonth) {
            return 3;
        } else {
            return 1;
        }
    }
}
